package com.gzx.plugin.tjzy.modular.fm.service.impl;

import com.gzx.plugin.tjzy.modular.fm.api.FmApi;
import com.gzx.plugin.tjzy.modular.fm.config.FmConfig;
import com.gzx.plugin.tjzy.modular.fm.dto.RingEventRequestDto;
import com.gzx.plugin.tjzy.modular.fm.service.FmService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class FmServiceImpl implements FmService {

    private final FmConfig fmConfig;

    private static final String FM_CONTENT = "【天津普惠金融调解中心】天津自贸试验区普惠金融调解中心给您来电，请您注意接听！";

    @Override
    public void sendFlashMessage(RingEventRequestDto ringEventRequestDto) {
        if (isSend(ringEventRequestDto)) {
            FmApi.sendMessage(fmConfig.getUserName(), fmConfig.getPassword(), fmConfig.getUrl(), List.of(ringEventRequestDto.getCalled()), FM_CONTENT);
        }
    }

    private boolean isSend(RingEventRequestDto ringEventRequestDto) {
        //workId不为空，主叫号码和坐席均在nacos配置
        return StringUtils.isNotBlank(ringEventRequestDto.getWorkid()) && fmConfig.getCaller().contains(ringEventRequestDto.getCaller()) && fmConfig.getUserAccount().contains(ringEventRequestDto.getWorkid());
    }
}
